<?

/***************************************************************************
 Jupiter Content System @ Jupiterportal.com
 Copyright (C) 2005 Cosmin Flavius (highstrike@gmail.com)

 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 2
 of the License, or (at your option) any later version.

 More Info About The Licence At http://www.gnu.org/copyleft/gpl.html
****************************************************************************/

//-----------------------------------------------------------------
//  Filter Acces Rightes
//-----------------------------------------------------------------

if(!isset($is_guest))
{ header("location: $PHP_SELF?i=16"); exit; }

//-----------------------------------------------------------------
//  Add the new user to the database
//-----------------------------------------------------------------

switch($a)
{
	//-----------------------------------------------------------------
	//  Do register
	//-----------------------------------------------------------------

	case 1:

		//-----------------------------------------------------------------
		//  Checks and overchecks
		//-----------------------------------------------------------------
		if(!isset($_SESSION["$security_session"]['terms_agreed'])){ header("location: $PHP_SELF?i=error"); exit; }
	    $key=substr($_SESSION['key'],0,5);
	    $number = $_REQUEST['number'];
	    if($number!=$key){ header("location: $PHP_SELF?i=154"); exit; }	    		
		if(!$regusername){ header("location: $PHP_SELF?n=modules/register&i=7"); exit; }
		if(!$regpassword){ header("location: $PHP_SELF?n=modules/register&i=8"); exit; }
		if($regpassword != $regpassword2){ header("location: $PHP_SELF?n=modules/register&i=11"); exit; }
		if(!$regemail){ header("location: $PHP_SELF?n=modules/register&i=12"); exit; }
		if(!eregi('^[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.([a-zA-Z]{2,4})$', $regemail)){ header("location: $PHP_SELF?n=modules/register&i=13"); exit; }

		if(!$regurl) $regurl = NULL;
		if(!$regflag) $regflag = NULL;
		if(!$reglocation) $reglocation = NULL;
		if($regday == 0 || $regmonth == 0 || $regyear == 0) $regage = NULL;
		else $regage = mktime(0,0,0,$regmonth,$regday,$regyear);

		//-----------------------------------------------------------------
		//  MYSQL checks
		//-----------------------------------------------------------------

		$check_user = $db->getLine("SELECT id FROM users WHERE username='".$db->escape($regusername)."'");
		$check_email = $db->getLine("SELECT id FROM users WHERE email='".$db->escape($regemail)."'");

		if($check_user != FALSE){ header("location: $PHP_SELF?n=modules/register&i=9"); exit; }
		if($check_email != FALSE){ header("location: $PHP_SELF?n=modules/register&i=10"); exit; }

		//-----------------------------------------------------------------
		//  Begin writing the information
		//-----------------------------------------------------------------

		$info['authorization'] = 1;
		$info['username'] = jcm_htmlspecialchars(jcm_stripslashes($regusername));
		$info['password'] = md5($regpassword);
		$info['email'] = jcm_htmlspecialchars(jcm_stripslashes($regemail));
		$info['url'] = jcm_htmlspecialchars(jcm_stripslashes($regurl));
		$info['age'] = $regage;
		$info['flag'] = $regflag;
		$info['location'] = jcm_htmlspecialchars(jcm_stripslashes($reglocation));
		$info['registered'] = time();
		$info['lastvisit'] = time();
		$info['forum_lastvisit'] = time();
		$info['ip'] = find_ip();
		$info['forumposts'] = 0;
		$info['hideemail'] = $reghideemail;
		$info['calendarbday'] = $regcalendarbday;

		if($config['verify_reg'] == 1)
		{
			$info['status'] = "activate";
			$info['multikey'] = generate_code(20);
			$info['actime'] = time();
		}
		else
		{
			$info['status'] = "ready";
			$info['multikey'] = NULL;
			$info['actime'] = NULL;
		}

		$db->insertRow("users",$info);

		//-----------------------------------------------------------------
		//  Send mail if the verification is on
		//-----------------------------------------------------------------

		if($config['verify_reg'] == 1) { $info['reason'] = "activate"; send_email($info); }

		//-----------------------------------------------------------------
		//  Add bday to event if 1 was selected
		//-----------------------------------------------------------------

		if($regcalendarbday == 1 && $regage != NULL)
		{
			$get_id = $db->getLine("SELECT id, username FROM users WHERE username = '".$db->escape($regusername)."' AND email = '".$db->escape($regemail)."'");

			$event_db_add['type'] = "bday";
			$event_db_add['user_id'] = $get_id['id'];
			$event_db_add['user_user'] = $get_id['username'];
			$event_db_add['day'] = $regday;
			$event_db_add['month'] = $regmonth;
			$event_db_add['year'] = date("Y");
			$event_db_add['message'] = "Birthday Event";

			$db->insertRow("events",$event_db_add);
		}

		//-----------------------------------------------------------------
		//  Redirect + Trick
		//-----------------------------------------------------------------

		if($config['verify_reg'] == 1) header("location: $PHP_SELF?i=14");
		else header("location: $PHP_SELF?i=15");

	break;

	//-----------------------------------------------------------------
	//  Do activation
	//-----------------------------------------------------------------

	case 2:

		//-----------------------------------------------------------------
		//  Check if the provided key exists and activate the account
		//-----------------------------------------------------------------

		$check_key = $db->getLine("SELECT id FROM users WHERE multikey='".$db->escape($key)."'");

		if($check_key != FALSE)
		{
			//-----------------------------------------------------------------
			//  Update user to ready
			//-----------------------------------------------------------------

			$info['status'] = "ready";
			$info['multikey'] = NULL;
			$info['actime'] = NULL;

			$db->updateRow("users",$info,"id = ".$db->escape($check_key['id'])."");

			//-----------------------------------------------------------------
			//  Redirect when finished
			//-----------------------------------------------------------------

			header("location: $PHP_SELF?i=19");
		}
		else header("location: $PHP_SELF?i=18");

	break;

	//-----------------------------------------------------------------
	//  Do forget password
	//-----------------------------------------------------------------

	case 3:

		switch($d)
		{
			//-----------------------------------------------------------------
			//  Upadte the user to status forgotten
			//-----------------------------------------------------------------

			case 1:

				//-----------------------------------------------------------------
				//  Check if the user, email are a match and send the mail + Trick
				//-----------------------------------------------------------------

				$check_info = $db->getLine("SELECT * FROM users WHERE username = '".$db->escape($fpwusername)."' AND email = '".$db->escape($fpwemail)."'");

				if($check_info != FALSE)
				{
					//-----------------------------------------------------------------
					//  Check if user is not ready or banned
					//-----------------------------------------------------------------

					if($check_info['status'] == "activate") { header("location: $PHP_SELF?i=17"); exit; }
					if($check_info['status'] == "banned") { header("location: $PHP_SELF?i=6"); exit; }

					//-----------------------------------------------------------------
					//  If the user is ready or forgot send him the email
					//-----------------------------------------------------------------

					if($check_info['status'] == "ready" || $check_info['status'] == "forgot")
					{
						//-----------------------------------------------------------------
						//  Update status to forgot password
						//-----------------------------------------------------------------

						$check_info['status'] = "forgot";
						$check_info['multikey'] = generate_code(20);
						$check_info['actime'] = time();

						$db->updateRow("users",$check_info,"id = ".$db->escape($check_info['id'])."");

						//-----------------------------------------------------------------
						//  Send Mail
						//-----------------------------------------------------------------

						$check_info['reason'] = "forgot";
						send_email($check_info);

						//-----------------------------------------------------------------
						//  Redirect
						//-----------------------------------------------------------------

						header("location: $PHP_SELF?i=20");
					}
				}
				else header("location: $PHP_SELF?n=modules/register&a=3&i=21");

			break;

			//-----------------------------------------------------------------
			//  Change the password and make the user ready
			//-----------------------------------------------------------------

			case 2:

				//-----------------------------------------------------------------
				//  Check if the provided key exists and modify the pass
				//-----------------------------------------------------------------

				$check_key = $db->getLine("SELECT id, status FROM users WHERE multikey = '".addslashes($db->escape($key))."'");

				if($check_key != FALSE)
				{
					//-----------------------------------------------------------------
					//  Check what kind of user was he + Update db + redirect
					//-----------------------------------------------------------------

					$info['password'] = md5($fpwpassword);
					$info['status'] = "ready";
					$info['multikey'] = NULL;
					$info['actime'] = NULL;

					$db->updateRow("users",$info,"id = ".$db->escape($check_key['id'])."");

					header("location: $PHP_SELF?i=22");
				}
				else header("location: $PHP_SELF?i=23");

			break;

			//-----------------------------------------------------------------
			//  Change the password Form
			//-----------------------------------------------------------------

			case 3:

				//-----------------------------------------------------------------
				//  Check if the provided key exists and show form
				//-----------------------------------------------------------------

				$check_key = $db->getLine("SELECT id FROM users WHERE multikey='".addslashes($db->escape($key))."'");

				if($check_key != FALSE)
				{
					?>
						<form method="post" action="<?= $PHP_SELF ?>?n=modules/register">
						<table class="main" cellspacing="1" cellpadding="4" width="100%">
						<tr class="head"> 
						<td colspan="2" class="head"><?= $language['Forgotten title'] ?></td>
						</tr>
						<tr>
						<td class="con1" width="42%" valign="middle"><span class="hilight"><?= $language['Forgotten desc3'] ?></span></td>
						<td class="con1" width="58%" valign="bottom"><input type="password" name="fpwpassword" style="width:100%" maxlength="25" class="box" tabindex="5"></td>
						</tr>
						<tr>
						<td class="con1"><input type="button" style="width:100" class="box" value="<?= $language['Forgotten desc4'] ?>" onClick="window.history.go(-1);" tabindex="8"></td>
						<td class="con1" align="right"><input type="submit" style="width:100" class="box" value="<?= $language['Forgotten desc5'] ?>" tabindex="7"></td>
						</tr>
						<input type="hidden" name="a" value="3">
						<input type="hidden" name="d" value="2">
						<input type="hidden" name="key" value="<?= $key ?>">
						</table></form>
					<?
				}
				else header("location: $PHP_SELF?i=23");

			break;

			//-----------------------------------------------------------------
			//  Show form for lost password
			//-----------------------------------------------------------------

			default:

				?>
					<form method="post" action="<?= $PHP_SELF ?>?n=modules/register">
					<table class="main" cellspacing="1" cellpadding="4" width="100%">
					<tr class="head"> 
					<td colspan="2" class="head"><?= $language['Forgotten title'] ?></td>
					</tr>
					<tr>
					<td class="con1" width="42%" valign="middle"><span class="hilight"><?= $language['Forgotten desc'] ?></span></td>
					<td class="con1" width="58%" valign="bottom"><input type="text" name="fpwusername" style="width:100%" maxlength="25" class="box" tabindex="5"></td>
					</tr>
					<tr>
					<td class="con1" valign="middle"><span class="hilight"><?= $language['Forgotten desc2'] ?></span></td>
					<td class="con1" valign="bottom"><input type="text" name="fpwemail" style="width:100%" maxlength="35" class="box" tabindex="6"></td>
					</tr>
					<tr>
					<td class="con1"><input type="button" style="width:100" class="box" value="<?= $language['Forgotten desc4'] ?>" onClick="window.history.go(-1);" tabindex="8"></td>
					<td class="con1" align="right"><input type="submit" style="width:100" class="box" value="<?= $language['Forgotten desc5'] ?>" tabindex="7"></td>
					</tr>
					<input type="hidden" name="a" value="3">
					<input type="hidden" name="d" value="1">
					</table></form>
				<?
		}

	break;

	//-----------------------------------------------------------------
	//  Show register form
	//-----------------------------------------------------------------

	default:

		//-----------------------------------------------------------------
		//  Check if the terms exists
		//-----------------------------------------------------------------

		if($config['terms_tg'] == 0) $_SESSION["$security_session"]['terms_agreed'] = 1;

		//-----------------------------------------------------------------
		//  Display the register form
		//-----------------------------------------------------------------

		if(isset($_SESSION["$security_session"]['terms_agreed']))
		{
			?>
				<form method="post" action="<?= $PHP_SELF ?>?n=modules/register">
				<table class="main" cellspacing="1" cellpadding="4" width="100%">
				<col width="40%"><col width="60%">
				<tr class="head"> 
				<td colspan="2" class="head"><?= $language['Register title'] ?></td>
				</tr>
				<tr class="bottom">
				<td height="21" colspan="2"><?= $language['Register title2'] ?></td>
				</tr>
				<tr>
				<td class="con1" valign="middle"><span class="hilight"><?= $language['Register desc'] ?></span></td>
				<td class="con1" valign="bottom"><input type="text" name="regusername" style="width:100%" maxlength="25" class="box" tabindex="5"></td>
				</tr>
				<tr>
				<td class="con1" valign="middle"><span class="hilight"><?= $language['Register desc2'] ?></span></td>
				<td class="con1" valign="bottom"><input type="password" name="regpassword" style="width:100%" maxlength="25" class="box" tabindex="6"></td>
				</tr>
				<tr>
				<td class="con1" valign="middle"><span class="hilight"><?= $language['Register desc3'] ?></span></td>
				<td class="con1" valign="bottom"><input type="password" name="regpassword2" style="width:100%" maxlength="25" class="box" tabindex="7"></td>
				</tr>
				<tr>
				<td class="con1" valign="middle"><span class="hilight"><?= $language['Register desc4'] ?></span></td>
				<td class="con1" valign="bottom"><input type="text" name="regemail" style="width:100%" maxlength="35" class="box" tabindex="8"></td>
				</tr>
				<tr class="bottom">
				<td height="21" colspan="2"><?= $language['Register title3'] ?></td>
				</tr>
				<tr>
				<td class="con1" valign="middle"><?= $language['Register desc5'] ?></td>
				<td class="con1" valign="bottom"><input type="text" name="regurl" style="width:100%" maxlength="35" class="box" tabindex="9"></td>
				</tr>
				<tr>
				<td class="con1" valign="middle"><?= $language['Register desc6'] ?><? if(isset($o)) echo "<a href='$PHP_SELF?n=modules/register'> {$language['Register desc7']}</a></td>"; else echo "<a href='$PHP_SELF?n=modules/register&o=1'> {$language['Register desc8']}</a></td>"; echo "<td class='con1' valign='bottom'>"; if(isset($o)) echo "<input type='text' name='reglocation' style='width:100%' maxlength='35' class='box' tabindex='10'>"; else echo "<select name='regflag' style='width:100%' class='box' tabindex='10'>".find_country()."</select>" ?></td>
				</tr>
				<tr>
				<td class="con1" valign="middle"><?= $language['Register desc9'] ?></td>
				<td class="con1" valign="bottom"><table cellspacing="0" cellpadding="0" width="100%"><tr class='empty'><td width='30%'><select name="regday" class="box" style="width:100%" tabindex='11'><option value='0'></option><? for ($i = 1; $i <= 31; $i++) echo "<option value='$i'>$i</option>\n" ?></select></td><td align='center' width='5%'>-</td><td width='30%'><select name="regmonth" class="box" style="width:100%" tabindex='12'><option value='0'></option><? for ($i = 1; $i <= 12; $i++) echo "<option value='$i'>{$months[$i]}</option>\n" ?></select></td><td align='center' width='5%'>-</td><td width='30%'><select name="regyear" class="box" style="width:100%" tabindex='13'><option value='0'></option><? for ($i = 1900; $i <= date("Y") - 5; $i++) echo "<option value='$i'>$i</option>\n" ?></select></td></tr></table></td>
				</tr>
				<tr>
				<td class="con1" valign="middle"><?= $language['Register desc10'] ?></td>
				<td class="con1" valign="bottom"><input type="radio" name="reghideemail" value="1" tabindex='14'>&nbsp;<?= $language['Register desc14'] ?>&nbsp;&nbsp;&nbsp;<input type="radio" name="reghideemail" value="0" checked tabindex='15'>&nbsp;<?= $language['Register desc15'] ?></td>
				</tr>
				<tr>
				<td class="con1" valign="middle"><?= $language['Register desc11'] ?></td>
				<td class="con1" valign="bottom"><input type="radio" name="regcalendarbday" value="1" checked tabindex='16'>&nbsp;<?= $language['Register desc14'] ?>&nbsp;&nbsp;&nbsp;<input type="radio" name="regcalendarbday" value="0" tabindex='17'>&nbsp;<?= $language['Register desc15'] ?></td>
				</tr>
				<tr class="bottom">
				<td height="21" colspan="2"><?= $language['Register desc18'] ?></td>
				</tr>				
				<tr>
				<td class="con1" valign="middle"><img src="/modules/captcha.php" alt="<?= $language['Register desc19'] ?>"></td>
				<td class="con1" valign="bottom"><input type="text" name="number" style="width:100%" maxlength="5" class="box" tabindex="9"></td>
				</tr>					
				<tr>
				<td class="con1"><input type="button" style="width:100" class="box" value="<?= $language['Register desc12'] ?>" onClick="window.history.go(-1);" tabindex="19"></td>
				<td class="con1" align="right"><input type="submit" style="width:100" class="box" value="<?= $language['Register desc13'] ?>" tabindex="18"></td>
				</tr>
				<input type="hidden" name="a" value="1">
				</table></form>
			<?

		}
		else
		{
			switch($d)
			{
				//-----------------------------------------------------------------
				//  Deploy value to terms_agreed
				//-----------------------------------------------------------------

				case 1:

					//-----------------------------------------------------------------
					//  Do it and redirect
					//-----------------------------------------------------------------

					$_SESSION["$security_session"]['terms_agreed'] = 1;
					header("location: $PHP_SELF?n=modules/register");

				break;

				default:

					//-----------------------------------------------------------------
					//  Show Terms and Conditions
					//-----------------------------------------------------------------
				
					?>
						<form method="post" action="<?= $PHP_SELF ?>?n=modules/register&d=1">
						<table class="main" cellspacing="1" cellpadding="4" width="100%">
						<col width="50%"><col width="50%">
						<tr class="head"> 
						<td colspan="2" class="head"><?= $language['Register title'] ?></td>
						</tr>
						<tr class="con1"> 
						<td colspan="2"><?= parse_message($config['terms_txt']) ?></td>
						</tr>
						<tr class="con1"> 
						<td align="right"><input type="button" style="width:100" class="box" value="<?= $language['Register desc16'] ?>" onClick="window.history.go(-1);"></td>
						<td align="left"><input type="submit" style="width:100" class="box" value="<?= $language['Register desc17'] ?>"></td>
						</tr>
						</form>
						</table>
					<?
			}
		}
}

?>